package supertrunfo;

import com.birosoft.liquid.LiquidLookAndFeel;
import daos.LoginDAOS;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import domain.Carta;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Login extends JFrame {

    private static ResourceBundle myResources;
    public static String LinguaAtual = "Português";
    private static final Logger log = Logger.getLogger(Login.class.getName());

    /** Creates new form Login */
    public Login() {
        try {

            Handler console = new ConsoleHandler();
            Handler file = new FileHandler("C:\\SuperTropa.log");

            /*Define que na consola apenas aparece log com nível superior ou
              a warning e no ficheiro deve aparecer o log de qualquer nível
             */

            console.setLevel(Level.INFO);
            file.setLevel(Level.ALL);
            //Define o formato de output do ficheiro como XML
            file.setFormatter(new SimpleFormatter());
            //Adiciona os handlers para ficheiro e console
            log.addHandler(file);
            log.addHandler(console);
            //Ignora os Handlers definidos no Logger Global
            log.setUseParentHandlers(false);
        }

        catch(IOException io){
            log.warning("O ficheiro hellologgin.xml não pode ser criado");
        }
        if (LinguaAtual.equals("Português")) {
           myResources =  ResourceBundle.getBundle("Message_pt_BR",Locale.getDefault());
        }
        else {
           myResources =  ResourceBundle.getBundle("Message_en_US",Locale.ENGLISH);
        }
        log.info("Tela de login iniciada.");
        initComponents();
        getRootPane().setDefaultButton(bEntrar);
        tLogin.setToolTipText(myResources.getString("login.infLogin"));
        lCriarLogin.setToolTipText(myResources.getString("login.infCriarLogin"));
        tSenha.setToolTipText(myResources.getString("login.infSenha"));
        bEntrar.setToolTipText(myResources.getString("login.infLogar"));
        bCancelar.setToolTipText(myResources.getString("login.infSair"));
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        tSenha = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        tLogin = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        bCancelar = new javax.swing.JButton();
        lLogin = new javax.swing.JLabel();
        lCriarLogin = new javax.swing.JLabel();
        lSenha = new javax.swing.JLabel();
        bEntrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tSenha.setBounds(120, 130, 179, 19);
        jLayeredPane1.add(tSenha, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("A Damn Mess", 0, 30));
        jLabel3.setText(myResources.getString("titulo.login"));
        jLabel3.setBounds(80, 30, 210, 34);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        tLogin.setBounds(120, 90, 179, 20);
        jLayeredPane1.add(tLogin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setForeground(new java.awt.Color(0, 0, 102));
        jLabel2.setText("____________");
        jLabel2.setBounds(250, 230, 112, 15);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bCancelar.setFont(new java.awt.Font("Showcard Gothic", 0, 14));
        bCancelar.setText(myResources.getString("login.cancelar"));
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });
        bCancelar.setBounds(190, 180, 120, 27);
        jLayeredPane1.add(bCancelar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lLogin.setFont(new java.awt.Font("Showcard Gothic", 0, 14));
        lLogin.setText(myResources.getString("login.login"));
        lLogin.setBounds(50, 90, 155, 18);
        jLayeredPane1.add(lLogin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lCriarLogin.setFont(new java.awt.Font("Showcard Gothic", 0, 14));
        lCriarLogin.setForeground(new java.awt.Color(0, 0, 255));
        lCriarLogin.setText(myResources.getString("login.criarLogin"));
        lCriarLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lCriarLoginMouseClicked(evt);
            }
        });
        lCriarLogin.setBounds(250, 220, 155, 18);
        jLayeredPane1.add(lCriarLogin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lSenha.setFont(new java.awt.Font("Showcard Gothic", 0, 14));
        lSenha.setText(myResources.getString("login.senha"));
        lSenha.setBounds(50, 130, 155, 18);
        jLayeredPane1.add(lSenha, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bEntrar.setFont(new java.awt.Font("Showcard Gothic", 0, 14));
        bEntrar.setText(myResources.getString("login.entrar"));
        bEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEntrarActionPerformed(evt);
            }
        });
        bEntrar.setBounds(60, 180, 110, 27);
        jLayeredPane1.add(bEntrar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stcI.jpg"))); // NOI18N
        jLabel1.setBounds(0, 0, 380, 260);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-390)/2, (screenSize.height-298)/2, 390, 298);
    }// </editor-fold>//GEN-END:initComponents

    private void bEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEntrarActionPerformed
        if(tLogin.getText().equalsIgnoreCase("administrador") && tSenha.getText().equalsIgnoreCase("adm123")){
            log.fine("Administrador logou no sistema.");
            new PrincipalAdm();
            //setVisible(false);
            this.hide();
        } else{
            try{
                if(LoginDAOS.Login(tLogin.getText(),tSenha.getText())==1 && !tLogin.getText().equals("") && !tSenha.getText().equals("")){
                    JOptionPane.showMessageDialog(null,myResources.getString("login.entrada"));
                    log.fine("Usuario logou no sistema.");
                    new Principal();
                    setVisible(false);
                } else{
                    JOptionPane.showMessageDialog(null,myResources.getString("login.erro"));
                    log.fine("Usuário não conseguiu logar no sitema.");
                    tLogin.setText("");
                    tSenha.setText("");
                }
            } catch(Exception erro){
                System.out.println(erro);
            }
        }
}//GEN-LAST:event_bEntrarActionPerformed

    private void lCriarLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lCriarLoginMouseClicked
        new CriarLogin(this,false);// TODO add your handling code here:
}//GEN-LAST:event_lCriarLoginMouseClicked

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        System.exit(0);// TODO add your handling code here:
}//GEN-LAST:event_bCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
try{
       UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        LiquidLookAndFeel.setLiquidDecorations(true, "mac");
       }catch(Exception e){

       }
                new Login().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bEntrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel lCriarLogin;
    private javax.swing.JLabel lLogin;
    private javax.swing.JLabel lSenha;
    private javax.swing.JTextField tLogin;
    private javax.swing.JPasswordField tSenha;
    // End of variables declaration//GEN-END:variables

}
